// System Include
#include <random>
// Local Include
#include "Utils.h"

void Utils::Write2DArrayToFile(const float* ToWriteMatrix, const int& W, const int& H, std::string FileName) {
    mkdir("./TempOutput");

    std::ofstream Output("./TempOutput/" + FileName);
    Output.precision(10);
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++)
            Output << ToWriteMatrix[i * W + j] << " ";
        Output << std::endl;
    }
    Output.close();
}

void Utils::WriteCuda2DArrayToFile(const float* ToWriteMatrix, float* ToReceiveMatrix, const int& W, const int& H, std::string FileName) {
    const int ArraySize = W * H;

	float* ReceiveMatrix = ToReceiveMatrix;
    if (ToReceiveMatrix == nullptr)
        ReceiveMatrix = new float[ArraySize];

	HANDLE_ERROR(cudaMemcpy(ReceiveMatrix, ToWriteMatrix, ArraySize * sizeof(float), cudaMemcpyDeviceToHost));
    Write2DArrayToFile(ReceiveMatrix, W, H, FileName);

	if (ToReceiveMatrix == nullptr)
		delete[] ReceiveMatrix;
}
